export default function ({ app }) {
  // 只在客户端环境执行
  if (process.client) {
    const DESIGN_WIDTH = 1920;
    const BASE_FONT_SIZE = 16;
    
    function setRemUnit() {
      const windowWidth = document.documentElement.clientWidth || document.body.clientWidth;
      const fontSize = (windowWidth / DESIGN_WIDTH) * BASE_FONT_SIZE;
      document.documentElement.style.fontSize = Math.min(fontSize, 180) + 'px';
    }
    
    // 防抖函数，优化性能
    function debounce(fn, delay = 50) {
      let timer = null;
      return function() {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
          fn.apply(this, arguments);
        }, delay);
      };
    }
    
    const debouncedSetRem = debounce(setRemUnit);
    
    // 初始化时设置一次
    setRemUnit();
    
    // 监听窗口大小变化和屏幕旋转事件
    window.addEventListener('resize', debouncedSetRem);
    window.addEventListener('orientationchange', debouncedSetRem);
  }
}